package com.htx.zqs.blesmartmask.utils;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.support.annotation.NonNull;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.bean.LocationData;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Xk
 * Created by lenovo on 2017/10/25.
 */
public class LocationHelper {
    private static final AMapLocationClient onceClient = new AMapLocationClient(MyApplication.appContext);


    static {
        AMapLocationClientOption  locationClientOption = new AMapLocationClientOption();
      //  locationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        locationClientOption.setOnceLocation(true);
        onceClient.setLocationOption(locationClientOption);
    }

    @NonNull
    private static LocationData getLocationData(AMapLocation aMapLocation) {
        LocationData locationData = new LocationData();
        locationData.country = aMapLocation.getCountry();
        locationData.province = aMapLocation.getProvince();
        locationData.city = aMapLocation.getCity();
        locationData.district = aMapLocation.getDistrict();
        locationData.gLat = aMapLocation.getLatitude();
        locationData.gLng = aMapLocation.getLongitude();
        locationData.gAddress = aMapLocation.getAddress();
        return locationData;
    }


    public static void startLocation(final OnLocationListener listener){
        onceClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if(aMapLocation!=null){
                    if(aMapLocation.getErrorCode() == 0){
                        LocationData locationData = getLocationData(aMapLocation);
                        if(listener!=null){
                            listener.onLocationComplete(locationData);
                        }
                    }else{
                        if(listener!=null){
                            listener.onLocationFailed();
                        }
                    }
                }else{
                    if(listener!=null){
                        listener.onLocationFailed();
                    }
                }
            }
        });
        onceClient.startLocation();
    }

    public interface OnLocationListener{
        void onLocationComplete(LocationData locationData);
        void onLocationFailed();
    }

    public  static LocationData googleLocation(Context context, double lng, double lat){
        Geocoder ge = new Geocoder(context);
        Log.e("gps", "run: 进来没2" + ge);

//        try {
//            addList = ge.getFromLocation(location.getLatitude(),location.getLongitude(),1);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        boolean present = Geocoder.isPresent();
          List<Address> addList = new ArrayList<>();
        LocationData locationData = new LocationData();
        Log.e("gps", "run: 进来没3" + present);

            try {
                addList = ge.getFromLocation(lng, lat, 1);
                locationData.province = addList.get(0).getAdminArea();
                locationData.city = addList.get(0).getLocality();
              //  locationData.city = addList.get(0).get();
                Log.e("gps", "run: 进来没4" + addList);
            } catch (IOException e) {
                Log.e("gps", "run: 进来没4erroe" + e);
                e.printStackTrace();
            }
        return locationData;
    }

}
